###############################################################################
# SuperLinks                                                                  #
# Written by Gregory Nolle (greg@nolle.co.uk)                                 #
# Copyright 2000-2003 by PlasmaPulse Solutions (http://www.plasmapulse.com)   #
###############################################################################
# Libraries/HTML/Tree.pm -> Tree HTML library                                 #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package HTML::Tree;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }

use strict;

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# Build subroutine
sub Build {
  my $self = shift;
  my %in = ("name" => "", "namelink" => {}, "icon" => "", "children" => [], @_);

  my $Return = <<HTML;
<script language="JavaScript">
  <!--
  function toggle(image, folder) {
    if (folder.style.display == "none") {
      image.src = image.src.replace("plus", "minus");
      folder.style.display = "block";
    } else {
      image.src = image.src.replace("minus", "plus");
      folder.style.display = "none";
    }
  }
  //-->
</script>
HTML

  $Return .= qq~<div class="tree_div"><img src="$SYSTEM->{'PUBLIC_URL'}/Images/Tree/$in{'icon'}" class="tree_icon"><font class="tree_label">~;
  $Return .= qq~<a href="$in{'namelink'}->{'url'}" target="$in{'namelink'}->{'target'}">~ if ($in{'namelink'}->{'url'});
  $Return .= qq~$in{'name'}~;
  $Return .= qq~</a>~ if ($in{'namelink'}->{'url'});
  $Return .= qq~</font></div>\n~;
  
  if (scalar(@{ $in{'children'} }) >= 1) {
    $Return .= qq~<div class="tree_div" style="display: block;">\n~;
    
    $self->{'FOLDER'} = 1;
    
    for (my $h = 0; $h <= $#{ $in{'children'} }; $h++) {
      my %child = %{ $in{'children'}->[$h] };

      $self->{'PREFIX'} = qq~  <div>~;
      $Return .= $self->myElement(%child, last => ($h == $#{ $in{'children'} } ? 1 : 0));
    }

    $Return .= qq~</div>\n~;
  }
    
  return $Return;
}

###############################################################################
# myElement subroutine
sub myElement {
  my $self = shift;
  my %in = ("name" => "", "iconlink" => {}, "namelink" => {}, "icon" => "", "open" => 0, "last" => 0, "children" => [], @_);

  my $Return = $self->{'PREFIX'};
  
  if (scalar(@{ $in{'children'} }) >= 1) {
    $in{'iconlink'}->{'url'} ||= "#";
    $Return .= qq~<a href="$in{'iconlink'}->{'url'}" target="$in{'iconlink'}->{'target'}"><img src="$SYSTEM->{'PUBLIC_URL'}/Images/Tree/tree_~.($in{'last'} ? "L" : "T").($in{'open'} ? "minus" : "plus").qq~.png" onClick="toggle(this, folder_$self->{'FOLDER'})"></a>~;
  } else {
    $Return .= qq~<img src="$SYSTEM->{'PUBLIC_URL'}/Images/Tree/tree_~.($in{'last'} ? "L" : "T").qq~.png">~;
  }
  
  $Return .= qq~<img src="$SYSTEM->{'PUBLIC_URL'}/Images/Tree/$in{'icon'}" class="tree_icon">~;
  $Return .= qq~<font class="tree_label">~;
  $Return .= qq~<a href="$in{'namelink'}->{'url'}" target="$in{'namelink'}->{'target'}">~ if ($in{'namelink'}->{'url'});
  $Return .= qq~$in{'name'}~;
  $Return .= qq~</a>~ if ($in{'namelink'}->{'url'});
  $Return .= qq~</font></div>\n~;

  if (scalar(@{ $in{'children'} }) >= 1) {
    $Return .= qq~<div class="tree_div" id="folder_$self->{'FOLDER'}" class="div" style="display: ~.($in{'open'} ? "block" : "none").qq~;">\n~;
    
    my $Prefix = qq~$self->{'PREFIX'}<img src="$SYSTEM->{'PUBLIC_URL'}/Images/Tree/tree_~.($in{'last'} ? "blank" : "I").qq~.png">~;
    $self->{'FOLDER'}++;

    for (my $h = 0; $h <= $#{ $in{'children'} }; $h++) {
      my %child = %{ $in{'children'}->[$h] };
      
      $self->{'PREFIX'} = $Prefix;
      $Return .= $self->myElement(%child, last => ($h == $#{ $in{'children'} } ? 1 : 0));
    }
    
    $Return .= qq~</div>\n~;
  }

  return $Return;
}

1;